

#import "ChangeFrequencyNoTimeModule.h"
#import "Imports.h"

@implementation ChangeFrequencyNoTimeModule

- Double:sender
{
	
		[TheModuleController RunCompactPanel:self];
    	[[TheModuleController CurrentSound:self] stop];
		if ([[TheModuleController CurrentSound:self] samplingRate]==SND_RATE_CODEC)
			{[self Double8];}
		else
			{[self Double16];}
		return self;
    
}



- Double8
{
	signed char *  TheArray = (signed char *)[[TheModuleController CurrentSound:self] data];
	int First=0;
	int Second;
	int FromCounter;
	int CrossingCounter;
	int Size=[[TheModuleController CurrentSound:self] dataSize];
	int ToCounter;
	int MidPoint;

	do
		{
		CrossingCounter=First+2;
		for (CrossingCounter=CrossingCounter+2; 		(CrossingCounter<Size-1) &&  
									!	 ( (int) TheArray[CrossingCounter] >=0 && 
									(int) TheArray[CrossingCounter+1]<=0) ;  CrossingCounter++);
		Second=CrossingCounter;
		if  ((Second-First)%2 !=0)
			{
			Second--;
			}
		ToCounter=First;
		for (FromCounter=First  ;   FromCounter<Second  ;  FromCounter+=2)
			{
			TheArray[ToCounter]=TheArray[FromCounter];
			ToCounter++;
			}
		MidPoint=First+((Second-First)/2);
		for (FromCounter=First; FromCounter<MidPoint  ;   FromCounter++)
			{
			TheArray[FromCounter+MidPoint-First]=TheArray[FromCounter];
			}
		First=Second;
		}
	while (Second<Size-2);
	[TheModuleController SoundChanged: [TheModuleController CurrentSound:self]];
 	return self;

}


- Double16
{
	signed int *  TheArray = (signed int *)[[TheModuleController CurrentSound:self] data];
	int First=0;
	int Second;
	int FromCounter;
	int CrossingCounter;
	int Size=[[TheModuleController CurrentSound:self] dataSize]/2;
	int ToCounter;
	int MidPoint;

	do
		{
		CrossingCounter=First+2;
		for (CrossingCounter=CrossingCounter+2; 		(CrossingCounter<Size-1) &&  
									!	 ( (int) TheArray[CrossingCounter] <=0 && 
									(int) TheArray[CrossingCounter+1]>=0) ;  CrossingCounter++);
		Second=CrossingCounter;
		if  ((Second-First)%2 !=0)
			{
			Second--;
			}
		ToCounter=First;
		for (FromCounter=First  ;   FromCounter<Second  ;  FromCounter+=2)
			{
			TheArray[ToCounter]=TheArray[FromCounter];
			ToCounter++;
			}
		MidPoint=First+((Second-First)/2);
		for (FromCounter=First; FromCounter<MidPoint  ;   FromCounter++)
			{
			TheArray[FromCounter+MidPoint-First]=TheArray[FromCounter];
			}
		First=Second;
		}
	while (Second<Size-2);
	[TheModuleController SoundChanged: [TheModuleController CurrentSound:self]];
 	return self;
}



- Halve:sender
{
    int n;
	int a=0;
	int b=0;
	int z=0;
	int start=0;
	int t;
	int Number=10;
    signed short *x;
    int l=[[TheModuleController CurrentSound:self] dataSize]/2;
	[TheModuleController RunCompactPanel:self];
   	[[TheModuleController CurrentSound:self] stop];
    x=(signed short *)  [[TheModuleController CurrentSound:self] data];
	for (n=0;n<l;n++)
	{if ((x[n+1]-x[n]>0) && (x[n]<=0) && (x[n+1]>0))
		{if (start==0) {start=1;z=n;}
		 if (b==Number) {a=n;b++;} else
			{if (b<Number*2) {b++;} else
				{for (t=a;t>z;t--)
					{x[2*t-z]=x[t];
					x[2*t-z-1]=x[t];
					}
				z=n;
				b=0;
				}
			}
		}
	}
	[TheModuleController SoundChanged: [TheModuleController CurrentSound:self]];
return self;
}


@end
